using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace KL.Standard.Core.Converter;

/// <summary>
/// 自定义字段属性 转换格式
/// </summary>
public class KLJsonContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var jsonProperty = base.CreateProperty(member, memberSerialization);
        #region 时间格式化
        if (jsonProperty.PropertyType == typeof(DateTime))
        {
            if (member.GetCustomAttribute<JsonDayAttribute>(true) != null)
            {
                jsonProperty.Converter = new DayJsonConverter2();
            }
            else
            {
                jsonProperty.Converter = new TimeJsonConverter2();
            }
            
        }
        #endregion

        #region 为空&默认值的处理
        if (member.GetCustomAttribute<JsonDefaultValueIgnoreAttribute>(true) != null)
        {
            jsonProperty.DefaultValueHandling = DefaultValueHandling.Ignore;
        }
        if (member.GetCustomAttribute<JsonNullIgnoreAttribute>(true) != null)
        {
            jsonProperty.NullValueHandling = NullValueHandling.Ignore;
        }
        

        #endregion
       

        // if (jsonProperty.PropertyType == typeof(DateTime))
        // {
        //     jsonProperty.Converter = new DateTimeZoneConverter();
        //     jsonProperty.ItemConverter = new DateTimeZoneConverter();
        //var objType = jsonProperty.DeclaringType;//所在类Type
        //jsonProperty.UnderlyingNa me;//属性名
        //jsonProperty.PropertyName = "aaa";//修改序列化后的属性名
        //var attributes = jsonProperty.AttributeProvider.GetAttributes(true);//获取属性上所有Attribute
        //jsonProperty.PropertyType = typeof(string);
        // }
        return jsonProperty;
    }
}